import request from '@/utils/request'

// 查询列表
export function queryTableData(query) {
  return request({
    url: '/timeAnalysis/machineHistory/StateAnalysisList',
    method: 'get',
    params: query
  })
}

const cellRenderer = (params) => {
  return `<div style="background: ${params.data.colorId}; width: 98%; height: 88%;">${params.value}</div>`
}
const commonColumn = [
  { headerName: "主线ID", field: "machineGroup", minWidth: 100, flex: 1 },
  { headerName: "父设备", field: "topId", minWidth: 100, flex: 1 },
  { headerName: "设备Id", field: "machineId", minWidth: 100, flex: 1 },
  // { headerName: "变更事件", field: "eventName", minWidth: 120, flex: 1 },
  // { headerName: "损失类型", field: "lossType", minWidth: 100, flex: 1 },
  { headerName: "开始时间", field: "createTime", minWidth: 150, flex: 1 },
  { headerName: "结束时间", field: "endTime", minWidth: 150, flex: 1 },
  { headerName: "持续时长", field: "keepTime", minWidth: 70, flex: 1 },
  { headerName: "停机区分", field: "shutdownType", minWidth: 70, flex: 1 },
  { headerName: "前状态", field: "preEptStatus", minWidth: 70, flex: 1 },
  { headerName: "现状态", field: "eptStatus", minWidth: 70, flex: 1, cellRenderer },
  { headerName: "硬件状态", field: "processStatus", minWidth: 70, flex: 1 },
  { headerName: "通信状态", field: "hardwareStatus", minWidth: 70, flex: 1 },
  { headerName: "RCode", field: "rcode", minWidth: 100, flex: 1 },
  { headerName: "Code描述", field: "description", minWidth: 80, flex: 1 },
  { headerName: "制造类型", field: "modelType", minWidth: 70, flex: 1 },
  { headerName: "设备类型", field: "machineType", minWidth: 70, flex: 1 },
  { headerName: "源设备ID", field: "fromId", minWidth: 100, flex: 1 },
  { headerName: "源设备类型", field: "fromType", minWidth: 100, flex: 1 }
]

export const allColumn = [
  ...commonColumn,
  { headerName: "备注信息", field: "newRemak", minWidth: 400, flex: 1, filter: 'agMultiColumnFilter' }
]

export const downColumn = [
  ...commonColumn,
  { headerName: "报警ID", field: "alarmId", minWidth: 70, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "发报警时间", field: "alarmSendTime", minWidth: 150, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "读报警时间", field: "alarmReadTime", minWidth: 150, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "消报警时间", field: "alarmEndTime", minWidth: 150, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "报警文本", field: "alarmText", minWidth: 400, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "读报警损失/秒", field: "readLossTime", minWidth: 100, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "消报警损失/秒", field: "clearLossTime", minWidth: 100, flex: 1, filter: 'agMultiColumnFilter' },
  { headerName: "备注信息", field: "newRemak", minWidth: 400, flex: 1, filter: 'agMultiColumnFilter' }
]

export const pmColumn = [
  ...commonColumn
]

commonColumn.forEach((item) => {
  item.filter = 'agMultiColumnFilter'
})

/* 稼动时间汇总 */
const summary = [
  { headerName: "设备ID", field: "machineId", width: 120 },
  { headerName: "生产", field: "t1000", minWidth: 60, flex: 1 },
  { headerName: "待料", field: "t1100", minWidth: 60, flex: 1, headerClass: 'custom-orange' },
  { headerName: "堵料", field: "t1200", minWidth: 60, flex: 1, headerClass: 'custom-orange' },
  { headerName: "无计划", field: "t1300", minWidth: 60, flex: 1, headerClass: 'custom-orange' },
  { headerName: "设备故障", field: "t2000", minWidth: 60, flex: 1 },
  { headerName: "Interlock", field: "t3000", minWidth: 70, flex: 1 },
  { headerName: "原材料故障", field: "t4100", minWidth: 70, flex: 1 },
  { headerName: "辅材料故障", field: "t4200", minWidth: 70, flex: 1 },
  { headerName: "应急报警", field: "t6112", minWidth: 70, flex: 1, headerClass: 'custom-blue' },
  { headerName: "生产5S", field: "t6111", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "一级保养", field: "t5100", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "定期PM", field: "t5200", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "设备非定期", field: "t6100", minWidth: 70, flex: 1, headerClass: 'custom-blue' },
  { headerName: "品质非定期", field: "t6200", minWidth: 70, flex: 1, headerClass: 'custom-blue' },
  { headerName: "改造改善", field: "t8000", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "实验借机", field: "t7000", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "材料更换", field: "t9100", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "版型变更", field: "t9200", minWidth: 60, flex: 1, headerClass: 'custom-blue' },
  { headerName: "异常PM", field: "t10000", minWidth: 60, flex: 1 },
  { headerName: "设备OFF", field: "off", minWidth: 60, flex: 1 }
]

const cellRendererSummary = (params) => {
  let _target = null
  Object.values(params.data).forEach((item) => {
    if(Number(item)){
      _target = _target + Number(item)
    }
  })
  return _target.toFixed(0)+'.00'
}

const cellRendererSummaryPercent = (params) => {
  let _target = null
  Object.values(params.data).forEach((item) => {
    if(Number(item.replace(/%/g, ""))){
      _target = _target + Number(item.replace(/%/g, ""))
    }
  })
  return _target.toFixed(0)+'.00%'
}

export const columnSummary = [
  ...summary,
  { headerName: "合计", field: "startStopStatus", minWidth: 60, flex: 1, cellRenderer: cellRendererSummary }
]

export const columnSummaryPercent = [
  ...summary,
  { headerName: "合计", field: "startStopStatus", minWidth: 60, flex: 1, cellRenderer: cellRendererSummaryPercent }
]